package com.yisu.auction.manager.core.service.impl;

import com.google.common.collect.Lists;
import com.yisu.auction.ewf.common.web.EasyUIGrid;
import com.yisu.auction.ewf.entity.Member;
import com.yisu.auction.ewf.entity.Show;
import com.yisu.auction.manager.core.mapper.MemberMapper;
import com.yisu.auction.manager.core.mapper.ShowMapper;
import com.yisu.auction.manager.core.service.IShowService;
import com.yisu.auction.manager.domain.request.ShowInfoPackage.ShareShowRequest;
import com.yisu.auction.manager.domain.util.RedisUtil;
import com.yisu.auction.manager.domain.vo.dataAnalysis.AnalysisImageVo;
import com.yisu.auction.manager.domain.vo.showInfoPackageVos.ShareShowVo;
import org.apache.calcite.linq4j.Linq4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * author: 黄萧林
 * Business Description：
 * createTime：2017/8/22
 */
@Service("IShowService")
public class ShowServiceImpl extends BaseService implements IShowService {
    @Resource
    private ShowMapper showMapper;
    @Resource
    private MemberMapper memberMapper;

    /**
     * author:黄萧林
     * Business Description:分页获取晒单信息
     *
     * @param shareShowRequest
     * @return
     */
    @Override
    public EasyUIGrid<ShareShowVo> getShareOrderInfoByPage(ShareShowRequest shareShowRequest) {
        List<ShareShowVo> shows = showMapper.getShareShowByPage(shareShowRequest);
        EasyUIGrid<ShareShowVo> gridData = new EasyUIGrid<>();
        gridData.setTotal(shareShowRequest.getTotalRecords());
        gridData.setRows(shows);
        return gridData;
    }


    /**
     * author:黄萧林
     * Business Description:改变晒单状态
     *
     * @param id
     * @return
     */
    @Override
    public Boolean changeStatus(Long id) {
        Show entity = showMapper.selectByPrimaryKey(id);
        Show show = new Show();
        show.setId(id);
        if (entity.getStatus() == 1) {
            show.setStatus(2);
        } else {
            show.setStatus(1);
        }
        return showMapper.updateByPrimaryKeySelective(show) > 0;
    }

    /**
     * author:批量添加晒单
     * Business Description:改变晒单状态
     *
     * @param shows
     * @return
     */
    @Override
    public boolean batchAddShow(List<Show> shows) {
        if (!CollectionUtils.isEmpty(shows)) {
            for (Show item : shows) {
                // TODO: 2017/9/8 晒单时间需要调整， 不能半夜调整
                Member member = memberMapper.getVirtualMemberByRandom();
                item.setMemberId(member.getId());
                item.setCreateDate(getWorkDate(10000));
                if (!StringUtils.isEmpty(item.getImages())) {
                    item.setImages(getImageUrl(Linq4j.asEnumerable(item.getImages().split(",")).toList()));
                }
                showMapper.insertSelective(item);
            }
        }
        return true;
    }

    protected Date getWorkDate(int randomNum) {
        Calendar date = Calendar.getInstance();
        date.setTime(new Date());
        Random random = new Random();
        date.add(Calendar.MINUTE, -random.nextInt(randomNum));
        Date thisDate = date.getTime();
        int hour = thisDate.getHours();
        if (hour >= 8 && hour <= 22) {
            return thisDate;
        } else {
            return getWorkDate(randomNum);
        }
    }

    /**
     * author:赵杰
     * Business Description:获取所有晒单列表
     *
     * @return
     */
    @Override
    public List<Show> getShowForAll() {
        return showMapper.getShowForAll();
    }

    @Override
    public boolean deleteShowImageByFileName(String fileName) {
        return deleteFile(fileName);
    }

    /**
     * author:赵杰
     * Business Description:每日评价操作
     *
     * @return
     */
    @Override
    public boolean evaluateForEveryDay() {
        Random random = new Random();
        List<Show> shows = showMapper.getShowForRandom(random.nextInt(20));
        if (!CollectionUtils.isEmpty(shows)) {
            for (Show showItem : shows) {
                Show updateModel = new Show();
                updateModel.setId(showItem.getId());
                updateModel.setCreateDate(getWorkDate(2880));
                showMapper.updateByPrimaryKeySelective(updateModel);
            }
        }
        return true;
    }

    /**
     * author: 赵杰
     * Business Description：获取商品主图信息
     *
     * @param images
     * @return
     */
    public String getImageUrl(List<String> images) {
        if (!CollectionUtils.isEmpty(images)) {
            List<String> ImagesArray = Lists.newArrayList();
            for (String stringItem : images) {
                if (!org.apache.commons.lang3.StringUtils.isEmpty(stringItem)) {
                    String path = uploadFileForLocation(new File(stringItem));
                    if (!org.apache.commons.lang3.StringUtils.isEmpty(path))
                        ImagesArray.add(path.trim());
                }
            }
            return org.apache.commons.lang3.StringUtils.join(ImagesArray, ",");
        } else {
            return "";
        }
    }
}
